<template>
    <div id="newsList">
        <!-- 页面头部 -->
        <HomePageHead></HomePageHead>
        <!-- 页面导航 -->
        <HomePageNavigation></HomePageNavigation>
        <!-- 广告1 -->
        <HomeAdvertising :imgurl="adImg1"></HomeAdvertising>

        <!-- 二级导航列表 -->
        <div class="nav2top" v-if="parent_name != ''"></div>
        <div class="nav2" v-if="parent_name != ''">
            <ul class="inner">
                <li class="home">
                    <NuxtLink :to="`/${parent_pinyin}/index.html`"> {{ parent_name }}</NuxtLink>
                </li>
                <li class="titleList" v-for="(item, index) in secondNav" :key="index">
                    <NuxtLink :href="getLinkPath(item)" :title="item.alias"
                        :target="item.is_url == 1 ? '_blank' : '_self'">
                        <span class="nav2_title">{{ item.alias }}</span>
                    </NuxtLink>
                </li>
            </ul>
        </div>
        <div class="nav2btm" v-if="parent_name != ''"></div>

        <!-- 面包屑导航 -->
        <div class="breadcrumb">
            <div class="inner">
                <span class="location">当前位置:</span>
                <el-breadcrumb :separator-icon="ArrowRight">
                    <el-breadcrumb-item>
                        <NuxtLink to="/">首页</NuxtLink>
                    </el-breadcrumb-item>
                    <el-breadcrumb-item v-if="parent_name != ''">
                        <NuxtLink :to="`/${parent_pinyin}/index.html`"> {{ parent_name }}</NuxtLink>
                    </el-breadcrumb-item>
                    <el-breadcrumb-item>{{ name }}</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
        </div>
        <!-- 资讯列表 -->
        <div class="newsList">
            <div class="inner">
                <div class="innerLeft">
                    <ul class="list">
                        <li v-for="(item, index) in newsList" :key="index">
                            <NuxtLink :href="getLinkPathDetail(item)" :title="item.alias">
                                <span class="listTitle">{{ item.title }}</span>
                                <span class="time">{{ getTime(item.updated_at, 'month', 1) }}</span>
                            </NuxtLink>
                        </li>
                    </ul>
                    <!-- 分页器 -->
                    <div class="pagination" v-if="total">
                        <el-pagination size="small" background layout="prev, pager, next" :total="total" class="mt-4"
                            v-model:page-size="pageSize" prev-text="上一页" next-text="下一页" :current-page="pageNum"
                            @current-change="changePage" />
                    </div>
                </div>
                <div class="innerRight">
                    <DetailHotNews></DetailHotNews>
                    <DetailHotNews2></DetailHotNews2>
                </div>
            </div>
        </div>

        <!-- 列表页广告二 -->
        <HomeAdvertising :imgurl="adImg2"></HomeAdvertising>

        <!-- 页面底部 -->
        <HomeFoot1></HomeFoot1>
    </div>
</template>

<script setup>
import { onMounted } from 'vue';
import { ElBreadcrumb, ElBreadcrumbItem, ElPagination, ElMessage } from 'element-plus'
import { ArrowRight } from '@element-plus/icons-vue'

// const nuxtApp = useNuxtApp();
// const axios = nuxtApp.$axios;


//1.获得路由id start ---------------------------------------->
const route = useRoute();
let articleId = 0;//路由id
let pageNum = ref(2);
let total = ref(1);
let pageSize = ref(20);

//获得当前的完整路径
const fullPath = route.path;
//拆分，取出来中间这一段，然后提取数字部分
const segments = fullPath.split('/');
const targetSegment = segments[1];
//const numberPart = targetSegment.match(/\d+$/)?.[0]; 
//let routeId = 20 //排除路径错误可以打开这个

//通过导航路径反向查询导航id
const getRouteId = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment,
    },
});
if (getRouteId.code == 200) {
    articleId = getRouteId.data.category_id
} else {
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    console.log("错误位置：通过url路径查询导航池id")
    console.log("后端错误反馈：", getRouteId.message)
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
}

//获得html前的数字
// const pageUrl = segments[2]; 
// const pageNumber = pageUrl.split('.')[0];
// console.log("当前URL中的页码：")
// console.log(pageNumber)//2
// pageNum.value = parseInt(pageNumber);
pageNum.value = parseInt(route.params.id);

//1.获得路由id end ---------------------------------------->

//2.页面数据 start ---------------------------------------->
//2.2新闻列表
const newsList = ref([]);
let newslists = async () => {
    const listData = await requestDataPromise('/web/getWebsiteArticleList', {
        method: 'GET',
        query: {
            'page': pageNum.value,
            'pageSize': pageSize.value,
            'catid': articleId
        },
    });
    if (listData.code == 200) {
        newsList.value = listData.data.rows;
        total.value = listData.data.count;
        console.log('news111111', newsList.value);

    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取新闻列表")
        console.log("后端错误反馈1111：", listData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
//获得列表
newslists();

//分页事件
let changePage = (value) => {
    console.log("当前页码", value);
    navigateTo(`/${targetSegment}/list-${value}.html`)
}

//2.3获得页面名称
const name = ref('')
let getPageName = async () => {
    const pageName = await requestDataPromise('/web/getOneWebsiteCategory', {
        method: 'GET',
        query: {
            'catid': articleId
        },
    });
    if (pageName.code == 200) {
        name.value = pageName.data.alias
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：设置页面标题")
        console.log("后端错误反馈：", pageName.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
//获得列表
getPageName();
//2.页面数据 end ---------------------------------------->
//3.二级栏目 start ---------------------------------------->
//3.1通过id获取父栏目
const parent_name = ref([]);
const parent_id = ref([]);
const parent_pinyin = ref("");
let getParentNav = async () => {
    const listData = await requestDataPromise('/web/getOneWebsiteCategory', {
        method: 'GET',
        query: {
            'catid': articleId
        },
    });
    if (listData.code == 200) {
        // console.log(111999)
        // console.log(listData.data);
        parent_name.value = listData.data.parent_name;
        parent_id.value = listData.data.parent_id;
        parent_pinyin.value = listData.data.parent_pinyin;
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取新闻列表")
        console.log("后端错误反馈：", listData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
    getSecondNav();
}
//获得父级栏目详情
getParentNav();

// 3.2获取二级栏目
const secondNav = ref([]);
let getSecondNav = async () => {
    const listData = await requestDataPromise('/web/getWebsiteModelCategory', {
        method: 'GET',
        query: {
            'placeid': 1,
            'pid': parent_id.value,
            'num': 8,
        },
    });
    console.log('listData', listData);
    // secondNav.value = listData.data;

    if (listData.code == 200) {
        secondNav.value = listData.data;
        console.log('secondNav', secondNav.value);
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取新闻列表")
        console.log("后端错误反馈：", listData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
//3.二级栏目 end ---------------------------------------->

//4.设置seo信息 start---------------------------------------->
//4.1 设置seo信息
const setData = await requestDataPromise('/web/getWebsiteCategoryHead', {
    method: 'GET',
    query: {
        'catid': articleId
    },
});

if (setData.code == 200) {
    let seoTitle = setData.data.seo_title;
    let seoDescription = setData.data.seo_description;
    let seoKeywords = setData.data.seo_keywords;
    let seoSuffix = setData.data.suffix;
    let seoName = setData.data.website_name;

    useSeoMeta({
        title: seoTitle + "_" + seoName + "_" + seoSuffix,
        meta: [
            { name: 'description', content: seoDescription + "_" + seoName + "_" + seoSuffix, tagPriority: 10 },
            { name: 'keywords', content: seoKeywords + "_" + seoName + "_" + seoSuffix, tagPriority: 10 }
        ]
    });
} else {
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    console.log("错误位置：设置列表页面SEO数据")
    console.log("后端错误反馈：", setData.message)
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
}
//4.设置seo信息 end---------------------------------------->

//5.广告 start---------------------------------------->
let adImg1 = ref({});
let adImg2 = ref({});

onMounted(async () => {
    //从客户端获取行政职能部门 加快打开速度
    const { $webUrl, $CwebUrl } = useNuxtApp();
    //广告1
    let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=xcw_list_0001`
    const responseAd1 = await fetch(url, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd1 = await responseAd1.json();
    adImg1.value = resultAd1.data[0];
    console.log('adImg1', adImg1.value);



    //广告2
    let url2 = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=xcw_list_0002`
    const responseAd2 = await fetch(url2, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd2 = await responseAd2.json();
    adImg2.value = resultAd2.data[0];


})
//5.广告 end---------------------------------------->

// 6.跳转 start ---------------------------------------->
const getLinkPathDetail = (item) => {
    if (item.islink == 1) {
        return `${item.linkurl}`;
    } else {
        return `/${item.pinyin}/${item.id}.html`;
    }
}
// 6.跳转 end ---------------------------------------->

</script>

<style lang="less" scoped>
#newsList {
    width: 100%;
    overflow: hidden;
}

.nav2top {
    width: 1250px;
    height: 3px;
    margin: 0 auto;
    background-color: #a01c0e;
}

.nav2btm {
    width: 1200px;
    height: 3px;
    margin: 0 auto;
    background-color: #a01c0e;
}

.nav2 {
    width: 100%;
    height: 48px;
    background-color: #a01c0e;

    .inner {
        width: 1250px;
        height: 48px;
        line-height: 48px;
        padding: 0 45px;
        margin: 0 auto;
        box-sizing: border-box;
        display: flex;
        // justify-content: space-between;

        .titleList {
            font-family: Microsoft YaHei, Microsoft YaHei;
            font-weight: bold;
            font-size: 14px;
            color: #FFFFFF;

            .nav2_title {
                display: inline-block;
                font-family: Microsoft YaHei, Microsoft YaHei;
                font-weight: bold;
                font-size: 14px;
                color: #FFFFFF;
            }

            a {
                padding: 0 38px;
                color: #FFFFFF;
                border-left: 1px solid #7E1C11;
            }

            //鼠标点击后的样式
            .nav2_title_active {
                display: inline-block;
                font-family: Microsoft YaHei, Microsoft YaHei;
                font-weight: bold;
                font-size: 14px;
                color: #FFFFFF;
                border-bottom: 1px solid #FFFFFF;
            }
        }

        .titleList:hover {
            a {
                color: #a01c0e;

                .nav2_title {
                    border-bottom: 1px solid #FFFFFF;
                }
            }
        }

        .home {
            width: 100px;
            font-family: Microsoft YaHei, Microsoft YaHei;
            font-weight: bold;
            font-size: 14px;
            color: #F8ECD2;

            a {
                color: #F8ECD2;
                margin-right: 39px;
            }
        }
    }
}

//导航条
.breadcrumb {
    width: 100%;
    height: 22px;
    margin-bottom: 30px;
    font-family: Microsoft YaHei, Microsoft YaHei;
    font-weight: 400;
    font-size: 20px;
    color: #666666;
    line-height: 23px;
    text-align: left;
    font-style: normal;
    text-transform: none;
    margin-top: 40px;

    :deep(.el-breadcrumb) {
        display: inline-block;
        vertical-align: -4px;
    }

    :deep(.el-breadcrumb__inner a),
    :deep(.el-breadcrumb__inner.is-link) {
        color: #666666;
        font-weight: 400;
        text-decoration: none;
        transition: var(--el-transition-color);
    }

    span {
        font-family: Microsoft YaHei, Microsoft YaHei;
        font-weight: 400;
        font-size: 20px;
        color: #666666;
        line-height: 23px;
        text-align: left;
        font-style: normal;
        text-transform: none;
    }

    span:hover {
        color: #666666;
    }

    .location {
        margin-right: 20px;
        width: 100px;
        height: 22px;
        font-family: Microsoft YaHei, Microsoft YaHei;
        font-weight: 400;
        font-size: 20px;
        color: #666666;
        line-height: 23px;
        text-align: left;
        font-style: normal;
        text-transform: none;
    }
}

// 资讯列表
.newsList {
    width: 100%;
    // height: 675px;
    overflow: hidden;
    margin-bottom: 70px;

    .inner {
        width: 1200px;

        .innerLeft,
        .innerRight {
            border-top: 1px solid #ccc;
        }

        .innerLeft {
            width: 820px;
            margin-right: 30px;

            .list {
                margin-bottom: 70px;

                li {
                    width: 820px;
                    padding: 15px 0;

                    a {
                        display: flex;
                        justify-content: space-between;
                    }

                    .listTitle {
                        // float: left;
                        display: inline-block;
                        width: 486px;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        font-family: Microsoft YaHei, Microsoft YaHei;
                        font-weight: 400;
                        font-size: 20px;
                        color: #333333;
                        text-align: left;
                        font-style: normal;
                        text-transform: none;
                    }

                    .time {
                        // float: right;
                        font-family: Microsoft YaHei, Microsoft YaHei;
                        font-weight: 400;
                        font-size: 18px;
                        color: #333333;
                    }

                }

                >li:hover .listTitle {
                    color: #a01c0e;
                }

                >li:nth-child(5n) {
                    border-bottom: 1px solid #D9D9D9;
                    padding-bottom: 30px;
                }

                >li:nth-child(5n+1) {
                    span {
                        font-weight: bold;
                    }

                    font-weight: bold;
                    padding-top: 30px;

                }
            }

            >.pagination {
                width: 800px;
                height: 34px;
                margin-left: 141px;
                display: flex;
                justify-content: center;
                margin: 0;

                // 鼠标移入后字体颜色
                .el-pagination::v-deep :hover {
                    color: #a01c0e;
                }

                .el-pagination.is-background::v-deep .btn-next,
                .el-pagination.is-background::v-deep .btn-prev {
                    width: 70px;
                    height: 34px;
                    margin: 0px 10px;
                    border-radius: 4px;
                }

                .el-pagination.is-background::v-deep .el-pager li {
                    margin: 0px 10px;
                    width: 38px;
                    height: 34px;
                    border-radius: 4px;
                }

                .el-pagination.is-background::v-deep .btn-next.is-active,
                .el-pagination.is-background::v-deep .btn-prev.is-active,
                .el-pagination.is-background::v-deep .el-pager li.is-active {
                    background-color: #a01c0e;
                    color: #fff;
                }


            }
        }

        .innerRight {
            width: 350px;
            height: 1242px;
            border-bottom: 1px solid #ccc;
            // height: 605px;
        }
    }
}

//资讯推荐
.zixuntuijian {
    width: 100%;
    height: 290px;
    margin-bottom: 70px;

    .innerLeft {

        // 左侧
        .zixunLeft {
            margin-right: 30px;
        }

        .zixunRight,
        .zixunLeft {
            float: left;
            width: 380px;
            height: 290px;

            // 标题部分
            >.title {
                width: 380px;
            }

            >.title>h3 {
                height: 36px;
                font-family: Source Han Sans, Source Han Sans;
                font-weight: bold;
                font-size: 24px;
                color: #000000;
                line-height: 28px;
                text-align: left;
                font-style: normal;
                text-transform: none;
                border-bottom: 1px solid #a01c0e;

            }

            >.title>h3>span {
                float: right;
                width: 56px;
                height: 20px;
                line-height: 24px;
                font-weight: 400;
                font-size: 14px;
                color: #999999;
                font-style: normal;
                text-transform: none;
            }

            .photo_text {

                >li:first-child {
                    width: 380px;
                    height: 120px;
                    margin-top: 20px;
                    margin-bottom: 15px;
                    position: relative;

                    >img {
                        float: left;
                        width: 160px;
                        height: 120px;
                    }

                    >div {
                        float: left;
                        width: 220px;
                        height: 120px;
                        padding-left: 15px;
                        padding-top: 6px;
                        box-sizing: border-box;
                        background-color: #f6f6f6;


                        >h5 {
                            width: 200px;
                            height: 54px;
                            display: -webkit-box;
                            -webkit-box-orient: vertical;
                            -webkit-line-clamp: 2;
                            overflow: hidden;
                            text-overflow: ellipsis;
                            word-break: break-all;
                            font-family: Source Han Sans, Source Han Sans;
                            font-weight: 500;
                            font-size: 18px;
                            color: #333333;
                            line-height: 26px;
                            text-align: left;
                            font-style: normal;
                            text-transform: none;
                        }

                        >p {
                            width: 200px;
                            height: 22px;
                            line-height: 20px;
                            position: absolute;
                            bottom: 5px;
                            right: 0;

                            >span {
                                display: inline-block;
                                // width: 100px;
                                height: 18px;
                                font-family: Source Han Sans, Source Han Sans;
                                font-weight: 400;
                                font-size: 12px;
                                color: #999999;
                                text-align: left;
                                line-height: 14px;
                                font-style: normal;
                                text-transform: none;
                            }

                            >span:last-child {
                                // width: 90px;
                                text-align: right;
                                margin-left: 20px;
                            }

                        }
                    }
                }

                >li {
                    width: 380px;
                    height: 25px;
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    font-family: PingFang SC, PingFang SC;
                    font-weight: 500;
                    font-size: 18px;
                    color: #333333;
                    line-height: 21px;
                    text-align: left;
                    font-style: normal;
                    text-transform: none;
                    margin-bottom: 10px;

                    em {
                        display: inline-block;
                        width: 8px;
                        height: 8px;
                        border-radius: 8px;
                        margin-right: 10px;
                        background-color: #d9d9d9;
                    }
                }

                >li:hover {
                    color: #a01c0e;
                }

                >li:hover em {
                    background-color: #a01c0e;
                }
            }
        }
    }

    .innerRight {
        width: 381px;
        height: 290px;
        background-color: #fbfbfb;

        >.title {
            width: 380px;
            height: 40px;
            line-height: 40px;
            border-top: 1px solid #a01c0e;
            border-bottom: 1px solid #e7e7e7;

            >h4 {
                font-family: Microsoft YaHei, Microsoft YaHei;
                font-weight: 400;
                margin-left: 20px;
                font-size: 20px;
                color: #000000;
                text-align: left;
                font-style: normal;
                text-transform: none;

                >span {
                    float: right;
                    font-family: Microsoft YaHei, Microsoft YaHei;
                    font-weight: 400;
                    font-size: 14px;
                    margin-right: 10px;
                    color: #999999;
                    text-align: left;
                    font-style: normal;
                    text-transform: none;
                }
            }

        }

        .rightList {
            height: 540px;
            margin-top: 20px;

            >li {
                height: 100px;
                margin-bottom: 10px;

                >img {
                    width: 150px;
                    height: 100px;
                }

                >p {
                    width: 219px;
                    height: 100px;
                    padding-left: 12px;
                    font-family: PingFang SC, PingFang SC;
                    font-weight: 400;
                    font-size: 16px;
                    color: #333333;
                    line-height: 22px;
                    text-align: left;
                    font-style: normal;
                    text-transform: none;

                }

                >p:hover {
                    box-shadow: 0 0 16px 1px rgba(0, 0, 0, 0.1);
                }
            }
        }

    }
}
</style>